package com.xiaolin.sports.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaolin.sports.common.Configuration;
import com.xiaolin.sports.common.RspResult;
import com.xiaolin.sports.common.enums.Status;
import com.xiaolin.sports.dao.SchoolDisCourseMapper;
import com.xiaolin.sports.dao.SchoolInfoMapper;
import com.xiaolin.sports.model.MyPutRet;
import com.xiaolin.sports.model.SchoolDisCourse;
import com.xiaolin.sports.model.SchoolInfo;
import com.xiaolin.sports.model.SysUser;
import com.xiaolin.sports.page.Page;
import com.xiaolin.sports.reqVo.UpdateIdsVO;
import com.xiaolin.sports.service.ISchoolInfoService;
import com.xiaolin.sports.utils.HttpClientUtils;
import com.xiaolin.sports.utils.JsonUtils;
import com.xiaolin.sports.utils.PropertiesUtil;
import com.xiaolin.sports.utils.ZTRuntimeException;
import com.xiaolin.sports.view.SchoolDisView;

@Service("schoolInfoService")
public class SchoolInfoServiceImpl implements ISchoolInfoService {

	@Autowired
	private SchoolInfoMapper schoolInfoMapper;
	@Autowired
	private SchoolDisCourseMapper schoolDisCourseMapper;
	
	@Override
	public void addAndUpdateSchoolInfo(SchoolInfo record, SysUser sysUser) {
		if(record.getId() == null) {
			record.setCreateUserId(sysUser.getIdUser());
			record.setCreater(sysUser.getUserName());
			schoolInfoMapper.insertSelective(record);
			String url = PropertiesUtil.getProperty(Configuration.getInstance().getEnv() + "_schoolPosterMsg");
			RspResult rspResult = null;
			try {
				String result = HttpClientUtils.postJson(url, record);
				rspResult = JsonUtils.jsonToObject(result, RspResult.class);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(rspResult != null && !rspResult.getCode().equals(Status.SUCCESS.getCode())) {
				throw new ZTRuntimeException("生成海报异常");
			}
			MyPutRet putRet = null;
			try {
				putRet = (MyPutRet)JsonUtils.mapToObject((LinkedHashMap<String, Object>)rspResult.getData(), MyPutRet.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(putRet == null) {
				throw new ZTRuntimeException("生成海报异常");
			}
			record.setSchoolEwm(putRet.getKey());
			schoolInfoMapper.updateByPrimaryKeySelective(record);
		}else {
			record.setUpdateUserId(sysUser.getIdUser());
			record.setUpdater(sysUser.getUserName());
			record.setUpdateTime(new Date());
			schoolInfoMapper.updateByPrimaryKeySelective(record);
		}
	}

	@Override
	public List<SchoolInfo> selectByPage(Page<SchoolInfo> page) {
		return schoolInfoMapper.selectByPage(page);
	}

	@Override
	public int updateBatchDel(UpdateIdsVO vo) {
		return schoolInfoMapper.updateBatchDel(vo);
	}

	@Override
	public List<SchoolDisView> selectSchoolDisList(Integer schoolId) {
		return schoolDisCourseMapper.selectSchoolDisList(schoolId);
	}

	@Override
	public void addAndUpdateSchoolDis(SchoolDisCourse record, SysUser sysUser) {
		if(record.getId() == null) {
			record.setCreateUserId(sysUser.getIdUser());
			record.setCreater(sysUser.getUserName());
			schoolDisCourseMapper.insertSelective(record);
		}else {
			record.setUpdateUserId(sysUser.getIdUser());
			record.setUpdater(sysUser.getUserName());
			record.setUpdateTime(new Date());
			schoolDisCourseMapper.updateByPrimaryKeySelective(record);
		}
	}

	@Override
	public int updateByPrimaryKeySelective(SchoolInfo record) {
		return schoolInfoMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public SchoolInfo selectById(Integer id) {
		return schoolInfoMapper.selectByPrimaryKey(id);
	}

}
